package zhuyun.code.datamanager.view

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.PopupMenu
import androidx.core.view.MenuItemCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import zhuyun.code.datamanager.R
import zhuyun.code.datamanager.databinding.ActivityMainBinding
import zhuyun.code.datamanager.model.Account
import zhuyun.code.datamanager.utils.UIUitls
import zhuyun.code.datamanager.viewmodel.AccountViewModel
import zhuyun.code.datamanager.viewmodel.ViewModelManager



class MainActivity : AppCompatActivity(),PopupMenu.OnMenuItemClickListener {
    var accountItems:List<out Account> = ArrayList()
    private var accountAdapter: AccountAdapter? = null
    private var mAccountViewModel:AccountViewModel? = null
    private var selectedPosition = 0
    private val mDataList = mutableListOf<Account?>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,
            R.layout.activity_main
        )

        mAccountViewModel = ViewModelManager<AccountViewModel>().getViewModel(this,AccountViewModel::class.java)
        accountAdapter = AccountAdapter(this,mDataList)
        binding.recyclerview.layoutManager = LinearLayoutManager(this);
        binding.recyclerview.adapter = accountAdapter;
        accountAdapter!!.setOnLongClickListener(object :AccountAdapter.OnLongClickListener{
            override fun onLongClick(v: View) {
                selectedPosition = v.tag as Int
                val popup = PopupMenu(this@MainActivity, v) //第二个参数是绑定的那个view
                //获取菜单填充器
                val inflater = popup.menuInflater
                //填充菜单
                inflater.inflate(R.menu.delete_or_modify_menu, popup.menu)
                //绑定菜单项的点击事件
                popup.setOnMenuItemClickListener(this@MainActivity)
                //显示(这一行代码不要忘记了)
                popup.show()
            }

        })
        //mAccountViewModel = ViewModelProviders.of(this).get(AccountViewModel::class.java)
        mAccountViewModel?.data?.observe(this,
            Observer<MutableList<Account?>> {
                mDataList.clear()
                mDataList.addAll(it)
                accountAdapter!!.notifyDataSetChanged()



                //mRecyclerAdapter.addHeaderView(getView());
//                val lineWidth = UIUitls.dip2px(this, 2.0f);
//                val horizontalDecoration = HorizontalDividerItemDecoration.Builder(this)
//                    .size(lineWidth)
//                    .colorResId(R.color.pink)
//                    .build()
//
//                binding.recyclerview.addItemDecoration(horizontalDecoration)
//                val simpleDividerDecoration = SimpleDividerDecoration(this)
//                val leftAndRightTagDecoration = LeftAndRightTagDecoration(this)
//                binding.recyclerview.addItemDecoration(simpleDividerDecoration)
//                binding.recyclerview.addItemDecoration(leftAndRightTagDecoration)

            })
        //MysqlDatabase(this).loadData()

    }

    override fun onResume() {
        super.onResume()
        mAccountViewModel!!.loadData()

    }

    private lateinit var tvAddAccount:TextView

    override fun onCreateOptionsMenu(menu: Menu):Boolean {
        menuInflater.inflate(R.menu.add_item_menu,menu);
        val menuItem = menu.findItem(R.id.addAccount)
        tvAddAccount = MenuItemCompat.getActionView(menuItem) as TextView
        tvAddAccount.text="增加"
        tvAddAccount.textSize = 20.0f
        tvAddAccount.setTextColor(resources.getColor(R.color.white))
        tvAddAccount.setPadding(0,0,UIUitls.dip2px(this,20.0f),0)
        tvAddAccount.setOnClickListener {
            startActivity(Intent(this@MainActivity,AddAccountActivity::class.java))
        }
        return true
    }
    override fun onOptionsItemSelected(item:MenuItem):Boolean{
        when (item.itemId){
            R.id.addAccount->{

            }

        }
        return true;
    }

    override fun onMenuItemClick(item: MenuItem?): Boolean {
        when(item!!.itemId){
            R.id.deleteAccount->{

                mDataList.removeAt(selectedPosition)
                accountAdapter?.notifyItemRemoved(selectedPosition)
            }
            R.id.modifyAccount->{

            }

        }
        return true
    }


}
